package org.bits.ui;

import java.awt.*;
import java.awt.Dialog.ModalityType;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.*;

import org.bits.common.IconConstants;
import org.jutils.Utils;

/*******************************************************************************
 * 
 ******************************************************************************/
public class OkCancelDialog
{
    /**  */
    private JDialog dialog;
    /**  */
    private JButton cancelButton;
    /**  */
    private JButton okButton;

    /**  */
    private boolean validate;
    /**  */
    private boolean okPressed;

    /***************************************************************************
     * @param owner
     * @param title
     * @param panel
     **************************************************************************/
    public OkCancelDialog( Window owner, String title, Component panel )
    {
        this( owner, title, panel, false, ModalityType.APPLICATION_MODAL );
    }

    /***************************************************************************
     * @param owner
     * @param title
     * @param panel
     * @param modality
     **************************************************************************/
    private OkCancelDialog( Window owner, String title, Component panel,
        boolean validate, ModalityType modality )
    {
        JPanel contentPanel = new JPanel( new GridBagLayout() );
        GridBagConstraints constraints;

        constraints = new GridBagConstraints( 0, 0, 1, 1, 1.0, 1.0,
            GridBagConstraints.CENTER, GridBagConstraints.BOTH, new Insets( 0,
                0, 0, 0 ), 0, 0 );
        contentPanel.add( panel, constraints );

        constraints = new GridBagConstraints( 0, 1, 1, 1, 1.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets( 0, 0, 0, 0 ), 0, 0 );
        contentPanel.add( new JSeparator(), constraints );

        constraints = new GridBagConstraints( 0, 2, 1, 1, 1.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.HORIZONTAL,
            new Insets( 0, 0, 0, 0 ), 0, 0 );
        contentPanel.add( createButtonPanel(), constraints );

        dialog = new JDialog( owner, title, modality );
        dialog.getRootPane().setDefaultButton( okButton );
        dialog.setContentPane( contentPanel );
        dialog.setLocationRelativeTo( owner );
    }

    /***************************************************************************
     * @param enabled
     **************************************************************************/
    public void setOkButtonEnabled( boolean enabled )
    {
        okButton.setEnabled( enabled );
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    private Component createButtonPanel()
    {
        JPanel buttonPanel = new JPanel( new GridBagLayout() );
        GridBagConstraints constraints;
        ButtonListener bl = new ButtonListener();

        cancelButton = new JButton( "Cancel",
            IconConstants.loader.getIcon( IconConstants.CANCEL_24 ) );
        cancelButton.addActionListener( bl );

        okButton = new JButton( "OK",
            IconConstants.loader.getIcon( IconConstants.OK_24 ) );
        okButton.addActionListener( bl );

        Utils.setMaxComponentSize( okButton, cancelButton );

        constraints = new GridBagConstraints( 0, 0, 1, 1, 1.0, 1.0,
            GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 10,
                10, 10, 30 ), 20, 10 );
        buttonPanel.add( Box.createHorizontalStrut( 0 ), constraints );

        constraints = new GridBagConstraints( 1, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 10,
                10, 10, 10 ), 20, 5 );
        buttonPanel.add( okButton, constraints );

        constraints = new GridBagConstraints( 2, 0, 1, 1, 0.0, 0.0,
            GridBagConstraints.CENTER, GridBagConstraints.NONE, new Insets( 10,
                10, 10, 10 ), 20, 5 );
        buttonPanel.add( cancelButton, constraints );

        return buttonPanel;
    }

    /***************************************************************************
     * @param width
     * @param height
     **************************************************************************/
    public void setSize( int width, int height )
    {
        dialog.setSize( width, height );
        validate = true;
    }

    /***************************************************************************
     * @param v
     **************************************************************************/
    public void setValidates( boolean v )
    {
        validate = v;
    }

    /***************************************************************************
     * @return
     **************************************************************************/
    public boolean showDialogView()
    {
        okPressed = false;

        if( validate )
        {
            dialog.validate();
        }
        else
        {
            dialog.pack();
        }
        dialog.setLocationRelativeTo( dialog.getOwner() );

        dialog.setVisible( true );

        return okPressed;
    }

    /***************************************************************************
     * 
     **************************************************************************/
    private class ButtonListener implements ActionListener
    {
        @Override
        public void actionPerformed( ActionEvent e )
        {
            if( e.getSource() == okButton )
            {
                okPressed = true;
            }
            else
            {
                okPressed = false;
            }
            dialog.dispose();
        }
    }
}
